package com.shuaitao.newsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuaitao.newsystem.bean.LoginUser;
import com.shuaitao.newsystem.bean.NewsUsers;
import com.shuaitao.newsystem.mapper.NewsUsersMapper;
import com.shuaitao.newsystem.service.NewsUsersService;
import com.shuaitao.newsystem.utils.JwtUtil;
import com.shuaitao.newsystem.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 24820
 * @description 针对表【news_users】的数据库操作Service实现
 * @createDate 2023-06-21 17:04:50
 */
@Service
public class NewsUsersServiceImpl extends ServiceImpl<NewsUsersMapper, NewsUsers>
        implements NewsUsersService {

    @Autowired
    @Lazy
    private AuthenticationManager authenticationManager;

    @Override
    public R login(String uname, String upwd, HttpSession session) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(uname, upwd);

        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("账号错误！");
        }
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        session.setAttribute("admin",loginUser);
        String jwt = JwtUtil.createJWT(loginUser.getUser().getUid().toString());
        Map<String, Object> map = new HashMap<>();
        map.put("authentication", jwt);
        map.put("user",loginUser);
        return R.ok(map);
    }

    @Override
    public R loginAdmin(NewsUsers newsUsers) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(newsUsers.getUname(),newsUsers.getUpwd());
        Authentication authenticate = null;
        try {
             authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("账号错误！");
        }
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
//        if (!loginUser.getPermissions().get(0).equals("ROLE_root")) {
//            throw new RuntimeException("账号错误！");
//        }
        String jwt = JwtUtil.createJWT(loginUser.getUser().getUid().toString());
        Map<String, Object> map = new HashMap<>();
        map.put("authentication", jwt);
        map.put("user",loginUser);
        return R.ok(map);
    }
}




